package com.cs2.volunteerpostbackend.functioncall;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class DatabaseManageTools {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Tool(description = "根据输入条件，对符合条件的文章进行点赞操作")
    public Map<String, String> likeSpecificMoments(String args) {
        System.out.println("=====================");
        System.out.println("=== Start likeSpecificMoments Function ===");

        System.out.println("model's sql ==> args: " + args);

        String updateSql = "UPDATE moment SET like_count = like_count + 1 WHERE " +
                args;

        // 执行 sql 语句
        int rows = jdbcTemplate.update(updateSql);
        System.out.println("点赞成功, 影响行数: " + rows);

        System.out.println("=== END likeSpecificMoments Function ===");
        return Map.of( "msg", "完成修改");
    }

    @Tool(description = "根据输入的条件，删除符合条件的动态操作")
    public Map<String, String> deleteSpecificMoments(String args) {
        System.out.println();
        System.out.println("=== Start deleteSpecificMoments Function ===");

        System.out.println("model's sql ==> args: " + args);

        String updateSql = "DELETE FROM moment  WHERE " +
                args;

        // 执行 sql 语句
        int rows = jdbcTemplate.update(updateSql);
        System.out.println("点赞成功, 影响行数: " + rows);

        System.out.println("=== END deleteSpecificMoments Function ===");
        return Map.of( "msg", "完成修改");
    }

    @Tool(description = "在需要根据时间进行排序，生成sql语句中需要使用limit时使用。先查询符合条件的动态，然后对查询结果进行点赞操作")
    public Map<String, String> likeSpecificMomentsWithQuery(String queryCondition, int limit) {
        System.out.println("=====================");
        System.out.println("=== Start likeSpecificMomentsWithQuery Function ===");

        System.out.println("查询条件: " + queryCondition);
        System.out.println("限制数量: " + limit);

        // 先查询符合条件的动态ID
        String selectSql = "SELECT id FROM moment WHERE " + queryCondition + " ORDER BY update_time DESC LIMIT " + limit;
        System.out.println("执行查询SQL: " + selectSql);

        List<String> momentIds = jdbcTemplate.query(selectSql, (rs, rowNum) -> rs.getString("id"));

        if (momentIds.isEmpty()) {
            System.out.println("未找到符合条件的动态");
            System.out.println("=== END likeSpecificMomentsWithQuery Function ===");
            return Map.of("msg", "未找到符合条件的动态");
        }

        System.out.println("查询到的动态ID: " + momentIds);

        // 构造更新语句，只更新查询到的动态
        StringBuilder idConditions = new StringBuilder();
        idConditions.append("id IN (");
        for (int i = 0; i < momentIds.size(); i++) {
            if (i > 0) {
                idConditions.append(",");
            }
            idConditions.append("'").append(momentIds.get(i)).append("'");
        }
        idConditions.append(")");

        String updateSql = "UPDATE moment SET like_count = like_count + 1 WHERE " + idConditions.toString();
        System.out.println("执行更新SQL: " + updateSql);

        // 执行更新操作
        int rows = jdbcTemplate.update(updateSql);
        System.out.println("点赞成功, 影响行数: " + rows);

        System.out.println("=== END likeSpecificMomentsWithQuery Function ===");
        return Map.of("msg", "完成修改，影响行数: " + rows);
    }
}
